package com.example.demo.clid;

import com.example.demo.dao1.ClassJpaDao;
import com.example.demo.dao1.StuJpaDao;
import com.example.demo.entity.Classa;
import com.example.demo.entity.Stucu;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class ThymelefController {
    @Resource
    ClassJpaDao classJpaDao;
    @Resource
    StuJpaDao stuJpaDao;

    public ThymelefController() {
    }

    @GetMapping({"/toLogninPage"})
    public String deleteJpa(Model model, int id) {
        Classa classa = (Classa)this.classJpaDao.findById(id).get();
        model.addAttribute("aaa", classa.getName());
        model.addAttribute("bbb", classa);
        return "login";
    }

    @GetMapping({"/toLogninPage2"})
    public String deleteJpa3(Model uu) {
        List<Stucu> stucu = this.stuJpaDao.findAll();
        uu.addAttribute("cc", stucu);
        return "stu";
    }

    @GetMapping({"/toLogninPage5"})
    public String deleteJpa(Model tt) {
        List<Classa> classa = this.classJpaDao.findAll();
        tt.addAttribute("gg", classa);
        return "login";
    }

    @GetMapping({"/toLogninPage22"})
    public String deleteJpa3(Model uu, @RequestParam(defaultValue = "") String name, @RequestParam(defaultValue = "0") int age) {
        List list;
        if (age == 0) {
            list = this.stuJpaDao.findByNameContaining(name);
        } else {
            list = this.stuJpaDao.findByNameContainingAndAge(name, age);
        }

        uu.addAttribute("ddd", name);
        uu.addAttribute("cc", list);
        return "stu";
    }

    @GetMapping({"/toLogninPage51"})
    public String deleteJpa(Model tt, String name, String dep) {
        List<Classa> classa = this.classJpaDao.findByNameContainingOrDep(name, dep);
        tt.addAttribute("gg", classa);
        tt.addAttribute("bbb", name);
        return "login";
    }

    @GetMapping({"/vue"})
    public String vue() {
        return "vue";
    }
}
